package com.battery.web.controller.system.battery;

import java.util.List;

import com.battery.common.enums.YesOrNoEnum;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.AppUserBO;
import com.battery.system.domain.VO.AppUserVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.AppUser;
import com.battery.system.service.battery.IAppUserService;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 用户Controller
 *
 * @author battery
 * @date 2021-01-20
 */
@Controller
@RequestMapping("/system/customer")
public class AppUserController extends BaseController {

    private String prefix = "system/appUser";

    @Autowired
    private IAppUserService appUserService;


    @RequiresPermissions("system:appUser:view")
    @GetMapping()
    public String appUser() {
        return prefix + "/appUser";
    }


    /**
     * 查询用户列表
     */
    @RequiresPermissions("system:appUser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppUserBO appUserBO) {
        startPage();
//        List<AppUser> list = appUserService.selectAppUserList(appUser);
        List<AppUserVO> list = appUserService.selectAppUserVOList(appUserBO);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("system:appUser:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppUserBO appUserBO) {
        List<AppUserVO> list = appUserService.selectAppUserVOList(appUserBO);
        ExcelUtil<AppUserVO> util = new ExcelUtil<AppUserVO>(AppUserVO.class);
        return util.exportExcel(list, "appUser");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:appUser:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppUser appUser) {
        return toAjax(appUserService.insertAppUser(appUser));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        AppUser appUser = appUserService.selectAppUserById(id);
        mmap.put("appUser", appUser);
        return prefix + "/edit";
    }


    /**
     * 冻结用户
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/frozen/{id}")
    public String frozen(@PathVariable("id") String id, ModelMap mmap) {
        AppUser appUser = appUserService.selectAppUserById(id);
        mmap.put("appUser", appUser);
        return prefix + "/frozen";
    }


    /**
     * 冻结用户
     */
    @RequiresPermissions("system:appUser:frozen")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/frozen")
    @ResponseBody
    public AjaxResult frozen(AppUser appUser) {
        if (appUser.getId() == null) {
            return error("主键ID为空");
        }
        if (StringUtils.isBlank(appUser.getReason())) {
            return error("请填写冻结原因！");
        }
        AppUser appUserUpdate = new AppUser();
        appUserUpdate.setId(appUser.getId());
        appUserUpdate.setReason(appUser.getReason());
        appUserUpdate.setIsFrozen(YesOrNoEnum.YES.getCode());
        appUserUpdate.setUpdateTime(DateUtils.getNowDate());
        return toAjax(appUserService.updateByPrimaryKeySelective(appUserUpdate));
    }


    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:appUser:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppUser appUser) {
        return toAjax(appUserService.updateAppUser(appUser));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("system:appUser:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(appUserService.deleteAppUserByIds(ids));
    }


    /**
     * 解冻用户
     *
     * @param id
     * @return
     */
    @RequiresPermissions("system:appUser:frozen")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/unfreeze")
    @ResponseBody
    public AjaxResult unfreeze(String id) {
        AppUser appUserUpdate = new AppUser();
        appUserUpdate.setId(id);
        appUserUpdate.setUpdateTime(DateUtils.getNowDate());
        appUserUpdate.setReason("");
        appUserUpdate.setIsFrozen(YesOrNoEnum.NO.getCode());
        return toAjax(appUserService.updateByPrimaryKeySelective(appUserUpdate));
    }
}
